﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Controls;
using System.IO;
using System.Drawing.Imaging;
using System.Reflection;
using System.Diagnostics;

namespace QrCodeApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Debug.Listeners.Add(new ConsoleTraceListener());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Debug.WriteLine(comboBox2.SelectedItem);
            Debug.WriteLine(comboBox3.SelectedItem);
            Color color1 = Color.FromName(comboBox2.SelectedItem.ToString());
            SolidBrush brush1 = new SolidBrush(color1);
            Color color2 = Color.FromName(comboBox3.SelectedItem.ToString());
            SolidBrush brush2 = new SolidBrush(color2);
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入文本");
                textBox1.Focus();
                return;
            }
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(textBox1.Text);
            Renderer renderer = new Renderer(Convert.ToInt32(comboBox1.SelectedItem), brush1, brush2);
            MemoryStream ms = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);
            pictureBox1.Image = Image.FromStream(ms);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowNewFolderButton = true;
            folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;
            folderBrowserDialog.SelectedPath = "C:";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入文本");
                textBox2.Focus();
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请选择路径");
                button3.Focus();
                return;
            }
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(textBox2.Text);
            Color color1 = Color.FromName(comboBox5.SelectedItem.ToString());
            SolidBrush brush1 = new SolidBrush(color1);
            Color color2 = Color.FromName(comboBox4.SelectedItem.ToString());
            SolidBrush brush2 = new SolidBrush(color2);
            Renderer renderer = new Renderer(18, brush1, brush2);
            renderer.CreateImageFile(qrCode.Matrix, textBox3.Text + "\\char.png", ImageFormat.Png);
            MessageBox.Show("保存成功");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //填充QR大小
            for (int i = 0; i < 22; i++)
            {
                comboBox1.Items.Add(i);
                comboBox6.Items.Add(i);
            }
            comboBox1.SelectedIndex = 18;
            comboBox6.SelectedIndex = 18;
            //Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            //Assembly asm = Assembly.Load("System.Drawing.dll");
            Type type = typeof(Brushes);
            PropertyInfo[] myProperties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo item in myProperties)
            {
                comboBox2.Items.Add(item.Name);
                comboBox3.Items.Add(item.Name);
                comboBox5.Items.Add(item.Name);
                comboBox4.Items.Add(item.Name);
            }
            comboBox2.SelectedIndex = 8;
            comboBox3.SelectedIndex = 137;
            comboBox5.SelectedIndex = 8;
            comboBox4.SelectedIndex = 137;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Button button = new Button();
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    button = button1;
                    break;
                case 1:
                    button = button2;
                    break;
            }
            this.AcceptButton = button;
        }
    }
}
